<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Runs XPath query on XML data</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="simplexmlelement.tostring.html">« SimpleXMLElement::__toString</a></li>
      <li style="float: right;"><a href="class.simplexmliterator.html">SimpleXMLIterator »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.simplexmlelement.html">SimpleXMLElement</a></li>
    <li>Runs XPath query on XML data</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="simplexmlelement.xpath" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SimpleXMLElement::xpath</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">SimpleXMLElement::xpath</span> &mdash; <span class="dc-title">Runs XPath query on XML data</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-simplexmlelement.xpath-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>SimpleXMLElement::xpath</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$expression</code></span>): <span class="type"><span class="type">array</span>|<span class="type">null</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   The <code class="literal">xpath</code> method searches the SimpleXML node for
   children matching the <abbr title="XML Path Language">XPath</abbr> <code class="parameter">expression</code>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-simplexmlelement.xpath-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">expression</code></dt>

     <dd>

      <p class="para">
       An XPath path
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-simplexmlelement.xpath-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns an <span class="type">array</span> of SimpleXMLElement objects on success; or <strong><code>null</code></strong> or <strong><code>false</code></strong> in
   case of an error.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-simplexmlelement.xpath-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5465">
    <p><strong>示例 #1 Xpath</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$string&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;XML<br /></span><span style="color: #DD0000">&lt;a&gt;<br />&nbsp;&lt;b&gt;<br />&nbsp;&nbsp;&lt;c&gt;text&lt;/c&gt;<br />&nbsp;&nbsp;&lt;c&gt;stuff&lt;/c&gt;<br />&nbsp;&lt;/b&gt;<br />&nbsp;&lt;d&gt;<br />&nbsp;&nbsp;&lt;c&gt;code&lt;/c&gt;<br />&nbsp;&lt;/d&gt;<br />&lt;/a&gt;<br /></span><span style="color: #007700">XML;<br /><br /></span><span style="color: #0000BB">$xml&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Search&nbsp;for&nbsp;&lt;a&gt;&lt;b&gt;&lt;c&gt;&nbsp;*/<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">xpath</span><span style="color: #007700">(</span><span style="color: #DD0000">'/a/b/c'</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$node</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'/a/b/c:&nbsp;'</span><span style="color: #007700">,</span><span style="color: #0000BB">$node</span><span style="color: #007700">,</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Relative&nbsp;paths&nbsp;also&nbsp;work...&nbsp;*/<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">xpath</span><span style="color: #007700">(</span><span style="color: #DD0000">'b/c'</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$node</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'b/c:&nbsp;'</span><span style="color: #007700">,</span><span style="color: #0000BB">$node</span><span style="color: #007700">,</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
/a/b/c: text
/a/b/c: stuff
b/c: text
b/c: stuff
</pre></div>
    </div>
    <div class="example-contents"><p>
     Notice that the two results are equal.
    </p></div>
   </div>
  </p>
 </div>


  <div class="refsect1 seealso" id="refsect1-simplexmlelement.xpath-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="simplexmlelement.registerxpathnamespace.html" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::registerXPathNamespace()</a> - Creates a prefix/ns context for the next XPath query</span></li>
    <li class="member"><span class="methodname"><a href="simplexmlelement.getdocnamespaces.html" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::getDocNamespaces()</a> - Returns namespaces declared in document</span></li>
    <li class="member"><span class="methodname"><a href="simplexmlelement.getnamespaces.html" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::getNamespaces()</a> - Returns namespaces used in document</span></li>
    <li class="member"><a href="simplexml.examples-basic.html" class="xref">Basic SimpleXML usage</a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>